package net.lookyou.boot.demo.controller;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UpdateLevelController {


    @GetMapping("/updateLogByName")
    public String updateLogByName(String loggerName, String level) {
        LoggerContext context = (LoggerContext)LoggerFactory.getILoggerFactory();
        Logger logger = context.getLogger(loggerName);
        String beforeLevel = logger.getLevel().levelStr;
        logger.setLevel(Level.valueOf(level));
        String afterLevel = logger.getLevel().levelStr;
        return String.format("类路径：%s, 更新前级别：%s, 更新后级别：%s", loggerName, beforeLevel, afterLevel);
    }
}
